﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFCouse
{
    /// <summary>
    /// TabControlWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TabControlWindow : Window
    {
        public TabControlWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //设置TabControl的选中项
            //tabPages.SelectedItem = tabPages.Items[1];//设置选中的选项卡
            tabPages.SelectedIndex = 2;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            object obj = (tabPages.SelectedItem as System.Windows.Controls.TabItem).Content;  //Frame  如果TabItem的内容是Frame
            //object obj = tabPages.SelectedContent;//选择页的内容对象
            string roleName = txtRoleName.Text;


        }

        private void btnChange_Click(object sender, RoutedEventArgs e)
        {
            if (tabPages.SelectedIndex < tabPages.Items.Count - 1)
                tabPages.SelectedIndex += 1;
            else
                tabPages.SelectedIndex = 0;
        }

        int currentIndex = 0;
        bool isflag = true;

        private void tabPages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectindex = ((System.Windows.Controls.Primitives.Selector)sender).SelectedIndex;
            if (!isflag)
                return;
            switch (selectindex)
            {
                case 0:
                    currentIndex = selectindex;
                    break;
                case 1:
                    currentIndex = selectindex;
                    break;
                case 2:
                    if (tabPages.SelectedIndex != currentIndex&& isflag)
                    {
                        isflag = false;   
                        Dispatcher.BeginInvoke(new Action(() => 
                        {
                            e.Handled = true;
                        }));
                        isflag = true;
                        tabPages.SelectedIndex = currentIndex;
                     
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
